package com.yjc.drygoods.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yjc.common.to.MemberCollectSkuTo;
import com.yjc.common.to.MemberTo;
import com.yjc.common.to.OrderItemTo;
import com.yjc.common.to.OrderTo;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;
import com.yjc.drygoods.product.dao.SkuInfoDao;
import com.yjc.drygoods.product.entity.SkuImagesEntity;
import com.yjc.drygoods.product.entity.SkuInfoEntity;
import com.yjc.drygoods.product.entity.SpuInfoDescEntity;
import com.yjc.drygoods.product.entity.SpuInfoEntity;
import com.yjc.drygoods.product.exception.PriceReductionException;
import com.yjc.drygoods.product.feign.MemberFeignService;
import com.yjc.drygoods.product.feign.OrderFeignService;
import com.yjc.drygoods.product.feign.SearchFeignService;
import com.yjc.drygoods.product.feign.WareFeignService;
import com.yjc.drygoods.product.service.*;
import com.yjc.drygoods.product.vo.*;
import com.yjc.drygoods.product.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.common.utils.Query;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.rmi.CORBA.Tie;

@Slf4j
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor threadPool;

    @Autowired
    WareFeignService wareFeignService;

    @Autowired
    SpuCommentService spuCommentService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    SearchFeignService searchFeignService;

    @Autowired
    OrderFeignService orderFeignService;

    /**
     * 主要是为了更新干货价格
     *
     * @param price
     */
    @Transactional
    @Override
    public void updateSkuInfoPrice(BigDecimal price, Long skuId) {
        //将降价价格同步成价格
        SkuInfoEntity skuInfo = new SkuInfoEntity();
        skuInfo.setSkuId(skuId);
        skuInfo.setPrice(price);
        skuInfo.setReducedPrice(price);
        //直接更新购物车中指定spuId购物项和es中指定spuId的干货的实际价格
        //1.更改所有用户的购物车中指定skuId的干货的价格
        R allMemberInfo = memberFeignService.getAllMemberInfo();
        if (allMemberInfo.getCode() != 0) {
            throw new RuntimeException("会员远程服务调用异常");
        }
        List<MemberTo> members = allMemberInfo.getData(new TypeReference<List<MemberTo>>() {
        });
        for (MemberTo member : members) {
            BoundHashOperations<String, Object, Object> cartOps = getCartOps(member.getId());
            String cartItemJson = (String) cartOps.get(skuId.toString());
            if (!StringUtils.isEmpty(cartItemJson)) {
                CartItemVo cartItemVo = JSON.parseObject(cartItemJson, CartItemVo.class);
                cartItemVo.setPrice(skuInfo.getReducedPrice());
                log.info("skuIdReducedPrice:{}", skuInfo.getReducedPrice());
                String s = JSON.toJSONString(cartItemVo);
                cartOps.put(skuId.toString(), s);
            }
        }
        //2，修改es中的价格
        List<SkuInfoEntity> resList = new ArrayList<>();
        resList.add(skuInfo);
        R r = searchFeignService.updateSkuPrice(resList);
        if (r.getCode() != 0) {
            throw new RuntimeException("检索远程服务调用异常");
        }
        this.updateBatchById(resList);

    }


    /**
     * 更新降价
     *
     * @param spuId
     * @param reducedPrice
     */
    @Transactional
    @Override
    public void updateReducedPrice(Long spuId, BigDecimal reducedPrice) {
        List<SkuInfoEntity> skuInfos = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        for (SkuInfoEntity skuInfo : skuInfos) {
            if (skuInfo.getPrice().compareTo(reducedPrice) == 0 || skuInfo.getPrice().compareTo(reducedPrice) == -1) {
                throw new PriceReductionException("sku干货原价：" + skuInfo.getPrice());
            }
        }
        //数据库需要修改的降价价格
        List<SkuInfoEntity> resList = skuInfos.stream().map(item -> {
            item.setReducedPrice(item.getPrice().subtract(reducedPrice));
            return item;
        }).collect(Collectors.toList());

        //直接更新购物车中指定spuId购物项和es中指定spuId的干货的实际价格
        //1.更改所有用户购物车中指定spuId下的所有skuId干货的价格
        R allMemberInfo = memberFeignService.getAllMemberInfo();
        if (allMemberInfo.getCode() != 0) {
            throw new RuntimeException("会员远程服务调用异常");
        }
        List<MemberTo> members = allMemberInfo.getData(new TypeReference<List<MemberTo>>() {
        });
        //1.1获取指定spuId下的所有的sku干货的skuId
        List<Long> skuIds = resList.stream().map(SkuInfoEntity::getSkuId).collect(Collectors.toList());
        //1.2修改价格
        Map<Long, BigDecimal> resMap = resList
                .stream()
                .collect(Collectors.toMap(SkuInfoEntity::getSkuId, SkuInfoEntity::getReducedPrice));
        for (Long skuId : skuIds) {
            for (MemberTo item : members) {
                BoundHashOperations<String, Object, Object> cartOps = getCartOps(item.getId());
                String cartItemJson = (String) cartOps.get(skuId.toString());
                if (!StringUtils.isEmpty(cartItemJson)) {
                    CartItemVo cartItemVo = JSON.parseObject(cartItemJson, CartItemVo.class);
                    cartItemVo.setPrice(resMap.get(skuId));
                    log.info("skuId:{}", resMap.get(skuId));
                    String s = JSON.toJSONString(cartItemVo);
                    cartOps.put(skuId.toString(), s);
                }
            }
        }


        //2，修改es中的价格
        R r = searchFeignService.updateSkuPrice(resList);
        if (r.getCode() != 0) {
            throw new RuntimeException("检索远程服务调用异常");
        }
        //修改数据库中的干货的价格
        this.updateBatchById(resList);
    }


    public BoundHashOperations<String, Object, Object> getCartOps(Long userId) {
        String cartKey = "drygoods:cart:" + userId;
        BoundHashOperations<String, Object, Object> cartHashOps = stringRedisTemplate.boundHashOps(cartKey);
        return cartHashOps;
    }

    @Override
    public Map<String, List<EChartsAttentionCountVo>> getTwoYearAttentions() throws ExecutionException, InterruptedException {
        Map<String, List<EChartsAttentionCountVo>> resMap = new HashMap<>();

        CompletableFuture<List<Object>> dataFuture = CompletableFuture.supplyAsync(() -> {
            R r = memberFeignService.getAllMemberInfo();
            if (r.getCode() != 0) {
                throw new RuntimeException("用户远程服务调用异常");
            }
            List<MemberTo> memberInfos = r.getData(new TypeReference<List<MemberTo>>() {
            });
            List<SkuInfoEntity> skuInfos = this.list();
            List<Object> arr = new ArrayList<>();
            arr.add(skuInfos);
            arr.add(memberInfos);
            return arr;
        },threadPool);

        //20-40岁年龄段的干货关注量
        CompletableFuture<Void> smallFuture = dataFuture.thenAcceptAsync((arr) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            List<EChartsAttentionCountVo> res = geteChartsAttentionCountVos(arr, sdf, calendar, -20, -20);
            log.info("tF:{}",res);
            resMap.put("tF", res);
        },threadPool);
        //40-60岁年龄段的干货关注量
        CompletableFuture<Void> bigFuture = dataFuture.thenAcceptAsync((arr) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            List<EChartsAttentionCountVo> res = geteChartsAttentionCountVos(arr, sdf, calendar, -40, -20);
            log.info("fS:{}",res);
            resMap.put("fS", res);
        },threadPool);
        CompletableFuture.allOf(smallFuture,bigFuture).get();
        return resMap;
    }

    private List<EChartsAttentionCountVo> geteChartsAttentionCountVos(List<Object> arr, SimpleDateFormat sdf, Calendar calendar, int nTime, int oTime) {
        Date time = null;
        Date oldTime = null;
        try {
            time = new Date();
            calendar.setTime(time);
            calendar.add(Calendar.YEAR, nTime);
            time = calendar.getTime();
            calendar.add(Calendar.YEAR, oTime);
            oldTime = calendar.getTime();
            log.info("time:{}", time);
            log.info("oldTime:{}", oldTime);
            time = sdf.parse(sdf.format(time));
            oldTime = sdf.parse(sdf.format(oldTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date finalTime = time;
        Date finalOldTime = oldTime;

        List<SkuInfoEntity> skuInfos = (List<SkuInfoEntity>) arr.get(0);
        List<MemberTo> memberInfos = (List<MemberTo>) arr.get(1);
        Map<Long, EChartsAttentionCountVo> ecMap = skuInfos.stream().map(item -> {
            EChartsAttentionCountVo eChartsAttentionCountVo = new EChartsAttentionCountVo();
            eChartsAttentionCountVo.setSkuId(item.getSkuId());
            eChartsAttentionCountVo.setSkuName(item.getSkuName());
            eChartsAttentionCountVo.setAttentionCount(0L);
            return eChartsAttentionCountVo;
        }).collect(Collectors.toMap(EChartsAttentionCountVo::getSkuId, item -> item));
        List<MemberTo> members = memberInfos.stream().filter(item -> {
            return (item.getBirth().compareTo(finalOldTime) == 1
                    && (item.getBirth().compareTo(finalTime) == -1 || item.getBirth().compareTo(finalTime) == 0));
        }).collect(Collectors.toList());
        for (MemberTo member : members) {
            List<MemberCollectSkuTo> memberCollectSkuTos = member.getMemberCollectSkuTos();
            for (MemberCollectSkuTo memberCollectSkuTo : memberCollectSkuTos) {
                ecMap.get(memberCollectSkuTo.getSkuId()).setAttentionCount(
                        ecMap.get(memberCollectSkuTo.getSkuId()).getAttentionCount() + 1
                );
            }
        }
        Collection<EChartsAttentionCountVo> values = ecMap.values();
        return values.stream().collect(Collectors.toList());
    }

    @Override
    public Map<String, List<EChartsSaleCountVo>> getSevenFiveDrygoods() throws ExecutionException, InterruptedException {
        Map<String, List<EChartsSaleCountVo>> resMap = new HashMap<>();
        CompletableFuture<List<Object>> dataFuture = CompletableFuture.supplyAsync(() -> {
            List<SkuInfoEntity> skuInfos = this.list();
            R r = orderFeignService.getAllOrderWithOrderItems();
            if (r.getCode() != 0) {
                throw new RuntimeException("订单远程服务调用异常");
            }
            List<OrderTo> orders = r.getData(new TypeReference<List<OrderTo>>() {
            });
            List<Object> arr = new ArrayList<>();
            arr.add(skuInfos);
            arr.add(orders);
            return arr;
        }, threadPool);

        //当天排名前五干货
        CompletableFuture<Void> nowFuture = dataFuture.thenAcceptAsync((arr) -> {
            Date time = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                time = new Date();
//                log.info("sdf:{}", sdf.format(time));
                time = sdf.parse(sdf.format(time));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            List<EChartsSaleCountVo> values = geteChartsSaleCountVos(arr, time, sdf);
//            log.info("nowFuture:{}", values);
            resMap.put("nowDay", values);
        }, threadPool);
        //上一天排名前五干货
        CompletableFuture<Void> oldOneFuture = dataFuture.thenAcceptAsync((arr) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            List<EChartsSaleCountVo> values = getTimeWithData(sdf, calendar, arr, -1);
//            log.info("oldOneFuture:{}", values);
            resMap.put("oldOneDay", values);
        }, threadPool);
        //上两天排名前五干货
        CompletableFuture<Void> oldTwoFuture = dataFuture.thenAcceptAsync((arr) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            List<EChartsSaleCountVo> values = getTimeWithData(sdf, calendar, arr, -2);
//            log.info("oldTwoFuture:{}", values);
            resMap.put("oldTwoDay", values);
        }, threadPool);
        //上三天排名前五干货
        CompletableFuture<Void> oldThreeFuture = dataFuture.thenAcceptAsync((arr) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            List<EChartsSaleCountVo> values = getTimeWithData(sdf, calendar, arr, -3);
//            log.info("oldThreeFuture:{}", values);
            resMap.put("oldThreeDay", values);
        }, threadPool);
        //上四天排名前五干货
        CompletableFuture<Void> oldFourFuture = dataFuture.thenAcceptAsync((arr) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            List<EChartsSaleCountVo> values = getTimeWithData(sdf, calendar, arr, -4);
//            log.info("oldFourFuture:{}", values);
            resMap.put("oldFourDay", values);
        }, threadPool);

        //异步全部完成才可返回
        CompletableFuture.allOf(nowFuture, oldOneFuture, oldTwoFuture, oldThreeFuture, oldFourFuture).get();
        return resMap;
    }

    /**
     * 设置时间
     *
     * @param sdf
     * @param calendar
     * @param arr
     * @param val
     * @return
     */
    private List<EChartsSaleCountVo> getTimeWithData(SimpleDateFormat sdf, Calendar calendar, List<Object> arr, int val) {
        Date time = null;
        try {
            time = new Date();
            calendar.setTime(time);
            calendar.add(Calendar.DAY_OF_MONTH, val);
            time = calendar.getTime();
//            log.info("time:{}", time);
            time = sdf.parse(sdf.format(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return geteChartsSaleCountVos(arr, time, sdf);
    }

    /**
     * 获取ECharts模型数据
     *
     * @param arr
     * @param time
     * @param sdf
     * @return
     */
    private List<EChartsSaleCountVo> geteChartsSaleCountVos(List<Object> arr, Date time, SimpleDateFormat sdf) {
        List<SkuInfoEntity> skuInfos = (List<SkuInfoEntity>) arr.get(0);
        Map<Long, EChartsSaleCountVo> ecMap = skuInfos.stream().map(item -> {
            EChartsSaleCountVo e = new EChartsSaleCountVo();
            e.setSkuId(item.getSkuId());
            e.setSkuName(item.getSkuName());
            e.setSaleCount(0.0);
            return e;
        }).collect(Collectors.toMap(EChartsSaleCountVo::getSkuId, item -> item));
        List<OrderTo> orders = (List<OrderTo>) arr.get(1);
        Date finalTime = time;
//        log.info("finalTime:{}", finalTime);
        List<OrderTo> orderTos = orders.stream().filter(order -> {
            Date date = null;
            try {
                date = sdf.parse(sdf.format(order.getModifyTime()));
//                log.info("date:{}", date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date.compareTo(finalTime) == 0 ? true : false;
        }).collect(Collectors.toList());
//        log.info("orderTos:{}", orderTos);
        for (OrderTo item : orderTos) {
            List<OrderItemTo> orderItemTos = item.getOrderItems();
            for (OrderItemTo orderItemTo : orderItemTos) {
                ecMap.get(orderItemTo.getSkuId())
                        .setSaleCount(
                                new BigDecimal(ecMap.get(orderItemTo.getSkuId())
                                        .getSaleCount() + "").add((new BigDecimal(orderItemTo.getSkuQuantity() + "")
                                        .multiply(new BigDecimal(orderItemTo.getWeight() + "")))).doubleValue());
            }
        }
        Collection<EChartsSaleCountVo> values = ecMap.values();
        List<EChartsSaleCountVo> res = values.stream()
//                .sorted(Comparator.comparing(EChartsSaleCountVo::getSaleCount, Comparator.reverseOrder()))
                .collect(Collectors.toList());
//        log.info("res:{}", res);
//        List<EChartsSaleCountVo> eChartsSaleCountVos = res.subList(0, 5);
//        log.info("eChartsSaleCountVos:{}", eChartsSaleCountVos);
        return res;
    }

    //    @Cacheable(value = {"sku"}, key = "'item:'+#root.args[0]")
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {

//        Long spuId = skuInfoEntity.getSpuId();
//        Long catalogId = skuInfoEntity.getCatalogId();
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1.sku基本信息获取 `pms_sku_info`
            SkuInfoEntity skuInfoEntity = this.getById(skuId);
            List<String> catelogNamePath = categoryService.findCatelogNamePath(skuInfoEntity.getCatalogId());
            SpuInfoEntity spuInfo = spuInfoService.getById(skuInfoEntity.getSpuId());
//            BrandEntity brandEntity = brandService.getById(skuInfoEntity.getBrandId());
//            skuInfoEntity.setBrandName(brandEntity.getName());
            skuInfoEntity.setCatalogNames(catelogNamePath);
            skuInfoEntity.setSpuName(spuInfo.getSpuName());
            skuItemVo.setSkuInfo(skuInfoEntity);
            return skuInfoEntity;
        }, threadPool);

        CompletableFuture<List<SkuImagesEntity>> skuImagesFuture = CompletableFuture.supplyAsync(() -> {
            //2.sku的图片信息 `pms_sku_images`
            List<SkuImagesEntity> skuImages = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuItemVo.setSkuImages(skuImages);
            return skuImages;
        }, threadPool);

//        void accept(T t);
        CompletableFuture<Void> skuSaleAttrValueFuture = skuInfoFuture.thenAcceptAsync((res) -> {
            //3.获取spu销售属性组合 `pms_sku_sale_attr_value`
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSkuSaleAttrsWithSkuIds(res.getSpuId());
            skuItemVo.setSkuItemSaleAttrVos(skuItemSaleAttrVos);
        }, threadPool);
//        void accept(T t);
        CompletableFuture<Void> spuInfoDescFuture = skuInfoFuture.thenAcceptAsync((res) -> {
            //4.获取spu的介绍 `pms_spu_info_desc`
            SpuInfoDescEntity byId = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setSpuInfoDesc(byId);
        }, threadPool);
//        void accept(T t);
        CompletableFuture<Void> productAttrValueFuture = skuInfoFuture.thenAcceptAsync((res) -> {
            //5.获取spu的规格参数信息 `pms_product_attr_value`
            List<SkuItemAttrGroupVo> skuItemAttrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuIdAndCatelogId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setSkuItemAttrGroupVos(skuItemAttrGroupVos);
        }, threadPool);

        CompletableFuture<Void> skuWareFuture = skuInfoFuture.thenAcceptAsync((res) -> {
            //6.获取当前商品是否有货
            Long spuId = res.getSpuId();
            boolean isSingleSkuWare = wareFeignService.getSingleSpuWare(spuId);
            skuItemVo.setHasStock(isSingleSkuWare);
        }, threadPool);


        CompletableFuture<Void> spuCommentsFuture = CompletableFuture.runAsync(() -> {
            //7.获取sku的所有评论信息
            List<SpuCommentEntity> spuComments = spuCommentService.list(new QueryWrapper<SpuCommentEntity>()
                    .eq("sku_id", skuId)
                    .eq("show_status", 1));
            skuItemVo.setSpuComments(spuComments);
        }, threadPool);

        CompletableFuture.allOf(skuImagesFuture, skuSaleAttrValueFuture, spuInfoDescFuture, productAttrValueFuture, skuWareFuture, spuCommentsFuture).get();

        return skuItemVo;
    }

    @Override
    public SpuInfoEntity getSpuInfoBySkuId(Long skuId) {
        SkuInfoEntity skuInfo = this.getById(skuId);
        SpuInfoEntity spuInfo = spuInfoService.getById(skuInfo.getSpuId());
        return spuInfo;
    }

    @Override
    public void updateSkuSaleCount(List<SkuInfoEntity> skuInfos) {
        for (SkuInfoEntity skuInfo : skuInfos) {

            this.baseMapper.updateSkuSaleCount(skuInfo.getSkuId(), skuInfo.getSaleCount() * skuInfo.getWeight());
        }
    }

    @Override
    public List<SkuInfoEntity> getBatchSkuInfo(List<Long> skuIds) {
        List<SkuInfoEntity> skuInfos = this.list(new QueryWrapper<SkuInfoEntity>().in("sku_id", skuIds));
        return skuInfos;
    }

    @Override
    public void updateAttentionCount(Map<String, Object> params) {
        long skuId = Long.parseLong((String) params.get("skuId"));
        if (Boolean.parseBoolean((String) params.get("flag"))) {
            this.baseMapper.updateAttentionCount(skuId, "+");
        } else {
            this.baseMapper.updateAttentionCount(skuId, "-");
        }
    }


    //    key: '华为',//检索关键字
//    catelogId: 0,
//    brandId: 0,
//    min: 0,
//    max: 0
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(i -> i.eq("sku_id", key).or().like("sku_name", key));
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            queryWrapper.eq("catalog_id", catelogId);
        }
        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)) {
            queryWrapper.ge("price", min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max) && max != "0") {
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(new BigDecimal("0")) == 1) {
                    queryWrapper.le("price", max);
                }
            } catch (Exception e) {

            }
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List<SkuInfoEntity> getAllSkuInfo() {
        return this.list();
    }

    @Override
    public List<SkuInfoEntity> getSkuInfosBySpuId(Long spuId) {
        return this.baseMapper.selectSkuInfosBySpuId(spuId);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBatchSkuInfos(List<SkuInfoEntity> skuInfoEntities) {
        this.saveBatch(skuInfoEntities);
    }

    @Override
    public void saveSkuInfos(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }


}